//
//  CZStatusBottomView.swift
//  GZWeibo666
//
//  Created by Apple on 15/11/28.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
//// MARK: -按钮点击代理
//protocol CZStatusBottomViewDelegate: NSObjectProtocol{
//    // 转发
//    func forwardButtonClick(indexPath : NSIndexPath)
//    // 评论
//    func commentButtonClick(indexPath : NSIndexPath)
//    // 点赞
//    func likeButtonClick()
//}
class CZStatusBottomView: UIView {
    /// 设置代理
//    weak var delegate: CZStatusBottomViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    // MARK: - 准备UI
    private func prepareUI() {
        // 添加点击事件
        // 转发点击事
        forwardButton.addTarget(self, action: "clickForwardButton", forControlEvents: UIControlEvents.TouchUpInside)
        // 评论点击事件
        commentButton.addTarget(self, action: "clickCommentButton", forControlEvents: UIControlEvents.TouchUpInside)
        // 点赞点击事件
        likeButton.addTarget(self, action: "clickLikeButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        backgroundColor = UIColor(white: 0.98, alpha: 1)
        
        // 添加子控件
        addSubview(forwardButton)
        addSubview(commentButton)
        addSubview(likeButton)
        addSubview(sImageViewFirst)
        addSubview(sImageViewSecond)
        
        // 添加约束
        /// 平铺子控件
        /// self: 父控件
        /// views: 哪些子控件要平铺
        /// insets边距
        self.ff_HorizontalTile([forwardButton, commentButton, likeButton], insets: UIEdgeInsetsZero)
        
        sImageViewFirst.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: forwardButton, size: nil)
        
        sImageViewSecond.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: commentButton, size: nil)
    }
    
    public var indexPath : NSIndexPath?
    
    // MARK: - 按钮点击事件

    //    数量的判断
    var status:CZStatus?{
        didSet{
            //            转发数
            if let reposts_count=status?.reposts_count{
                if reposts_count != 0{
                    
                    forwardButton.setTitle("\(reposts_count)", forState: UIControlState.Normal)
                }
            }
            
            //            评论数
            if let comments_count=status?.comments_count{
                
                if comments_count != 0{
                    commentButton.setTitle("\(comments_count)", forState: UIControlState.Normal)
                }
                
            }
            
            
            //            点赞数
            if let attitudes_count=status?.attitudes_count{
                
                if attitudes_count==0{
                    
                    likeButton.setTitle("赞", forState: UIControlState.Normal)
                }else{
                    likeButton.setTitle("\(attitudes_count)", forState: UIControlState.Normal)
                }
                
            }
            
        }
    }
    // MARK: - 按钮点击事件
    func clickForwardButton(){
        forwardBlock(CZComposeViewControllerType.Forward)
        //        print("转发")
        
    }
    
    func clickCommentButton() {
        commentBlock(CZComposeViewControllerType.Comment)//         print("评论")
    }
    
    func clickLikeButton() {
       likeBlock()
        //      print("点赞")
    }
    
    var forwardBlock = {(action: CZComposeViewControllerType) ->Void in
        
    }
    var commentBlock = {(action: CZComposeViewControllerType) ->Void in
        
    }
    var likeBlock = {() ->Void in
        
    }
    // MARK: - 懒加载
    /// 转发按钮
    private lazy var forwardButton: UIButton = UIButton(imageName: "timeline_icon_retweet", title: "转发")
    
    /// 评论按钮
    private lazy var commentButton: UIButton = UIButton(imageName: "timeline_icon_comment", title: "评论")
    
    /// 赞按钮
    private lazy var likeButton: UIButton = UIButton(imageName: "timeline_icon_unlike", title: "赞")
    
    /// 分割线1
    private lazy var sImageViewFirst: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line_highlighted"))
    
    /// 分割线2
    private lazy var sImageViewSecond: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line_highlighted"))
}
